<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>About me</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <pan-thumb :image="user.avatar" :height="'100px'" :width="'100px'" :hoverable="false">
          <div>Hello</div>
          {{ user.name }}
        </pan-thumb>
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ user.name }}</div>
        <div class="user-role text-center text-muted">{{ user.username }}</div>
      </div>
    </div>


    <div class="user-bio">
      <div class="user-education user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="education" /><span>Education</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            <el-form label-width="60px" label-position="left" size="small">
              <el-form-item label="学校：">韶关学院</el-form-item>
              <el-form-item label="学院：">{{user.dept}}</el-form-item>
              <el-form-item label="专业：">{{user.major}}</el-form-item>
            </el-form>
          </div>
        </div>
      </div>

      <div class="user-skills user-bio-section">
        <el-upload
                class="upload-demo"
                action="http://upload.likeyunba.com/"
                :auto-upload="false"
                :file-list="fileList"
                :limit="1"
                :on-change="handleChange"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
                list-type="picture">
          <el-button size="small" type="primary">更换头像</el-button>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <el-button size="small" type="success"  @click="test" style="margin-top: 5px" v-loading.fullscreen.lock="fullscreenLoading">上传</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
import PanThumb from '@/components/PanThumb'
import axios from "axios";

export default {
  components: { PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          avatar: '',
          role: ''
        }
      }
    }
  },
  data(){
    return{
      fileList:[],
      file:"",
      fullscreenLoading : false
    }
  },
  methods:{
    handleAvatarSuccess(res, file) {
      // console.log(res)
      // console.log(file)
      this.imageUrl = URL.createObjectURL(file.raw);
      // this.user.userface = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
      console.log(file)
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    test(){
      if(this.fileList.length<=0){
        this.$message.warning("请选择上传文件！")
      }else{
        this.fullscreenLoading = true
        this.file=this.fileList[0].raw
        const axiosInstance = axios.create({});
        let data = new FormData();
        let user = JSON.parse(window.sessionStorage.getItem('user'))
        if(this.beforeAvatarUpload(this.fileList[0].raw)){
          data.append("file",this.fileList[0].raw);
          data.append("userId",user.id);
          axiosInstance({
            method:'post',
            url:'/info/',
            data:data,
          }).then(resp=>{
            if (resp){
              window.sessionStorage.removeItem("user")
              this.user.avatar = URL.createObjectURL(this.fileList[0].raw);
              window.sessionStorage.setItem("user",JSON.stringify(this.user))
              this.$message.success("上传成功！")
              this.fileList = []
              this.$router.replace('/userCenter')
              this.fullscreenLoading = false
            }
          }).catch(e=>{
            this.$message.error("上传失败！")
            this.fullscreenLoading = false
          })
        }
      }
      // console.log(this.fileList)
      // this.$refs.upload.submit();

    },
    handleChange(file, fileList) {
      this.fileList = fileList
    },
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 0px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
